package com.hua.common.commontools;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;

import java.util.List;

/**
 * d短信工具类
 * Created by hxl on 2016/4/11.
 */
public class SmsUtil {


    public static boolean sendSms(Context context, String msg, String telNo) {
        boolean sendOk = false;
        try {
            String SENT_SMS_ACTION = "SENT_SMS_ACTION";
            // 监听短信是否发生成功
            String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
            // create the deilverIntent parameter
            Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
            PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0, deliverIntent, 0);
            Intent sentIntent = new Intent(SENT_SMS_ACTION);
            PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent, 0);
            // 直接调用短信接口发短信
            SmsManager smsManager = SmsManager.getDefault();
            List<String> divideContents = smsManager.divideMessage(msg);
            for (String text : divideContents) {
                smsManager.sendTextMessage(telNo, null, text, sentPI, deliverPI);
            }
        } catch (Exception e) {
            sendOk = false;
        }
        return sendOk;
    }

}
